package cn.iocoder.yudao.module.crm.controller.admin.course;

import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CourseMaterialPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CourseMaterialRespVO;
import cn.iocoder.yudao.module.crm.controller.admin.course.vo.CourseMaterialSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.dal.dataobject.coursematerial.CourseMaterialDO;
import cn.iocoder.yudao.module.crm.service.coursematerial.CourseMaterialService;

@Tag(name = "管理后台 - 课程课件")
@RestController
@RequestMapping("/crm/course-material")
@Validated
public class CourseMaterialController {

    @Resource
    private CourseMaterialService courseMaterialService;

    @PostMapping("/create")
    @Operation(summary = "创建课程课件")
    @PreAuthorize("@ss.hasPermission('crm:course-material:create')")
    public CommonResult<Long> createCourseMaterial(@Valid @RequestBody CourseMaterialSaveReqVO createReqVO) {
        return success(courseMaterialService.createCourseMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程课件")
    @PreAuthorize("@ss.hasPermission('crm:course-material:update')")
    public CommonResult<Boolean> updateCourseMaterial(@Valid @RequestBody CourseMaterialSaveReqVO updateReqVO) {
        courseMaterialService.updateCourseMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程课件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:course-material:delete')")
    public CommonResult<Boolean> deleteCourseMaterial(@RequestParam("id") Long id) {
        courseMaterialService.deleteCourseMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程课件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:course-material:query')")
    public CommonResult<CourseMaterialRespVO> getCourseMaterial(@RequestParam("id") Long id) {
        CourseMaterialDO courseMaterial = courseMaterialService.getCourseMaterial(id);
        return success(BeanUtils.toBean(courseMaterial, CourseMaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程课件分页")
    @PreAuthorize("@ss.hasPermission('crm:course-material:query')")
    public CommonResult<PageResult<CourseMaterialRespVO>> getCourseMaterialPage(@Valid CourseMaterialPageReqVO pageReqVO) {
        PageResult<CourseMaterialDO> pageResult = courseMaterialService.getCourseMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseMaterialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程课件 Excel")
    @PreAuthorize("@ss.hasPermission('crm:course-material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseMaterialExcel(@Valid CourseMaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseMaterialDO> list = courseMaterialService.getCourseMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程课件.xls", "数据", CourseMaterialRespVO.class,
                        BeanUtils.toBean(list, CourseMaterialRespVO.class));
    }

}